package com.zzyl.controller;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/bed")
@Api(tags = "床位管理相关接口")
public class BedController extends BaseController {

    @Autowired
    private BedService bedService;

    @GetMapping("/read/room/{roomId}")
    @ApiOperation(value = "根据房间id查询床位", notes = "传入房间id")
    public ResponseResult<List<BedVo>> readBedByRoomId(
            @ApiParam(value = "房间ID", required = true) @PathVariable("roomId") Long roomId) {
        List<BedVo> beds = bedService.getBedsByRoomId(roomId);
        return success(beds);
    }

    /**
     * 创建床位
     *
     * @param bedDto
     * @return
     */
    @PostMapping("/create")
    @ApiOperation(value = "创建床位", notes = "床位数据对象")
    public ResponseResult createBed(@RequestBody BedDto bedDto) {
        //对象的转换 dto-> entity
        // Spring 提供的工具 BeanUtils
        // Bed bed = new Bed();
//        BeanUtils.copyProperties(bedDto,bed);
        Bed bed = BeanUtil.toBean(bedDto, Bed.class);
        bedService.createBed(bed);
        return success();
    }

    /**
     * 根据ID查询床位
     * @param id
     * @return
     */
    @GetMapping("/read/{id}")
    public ResponseResult readBed(@PathVariable("id") Long id) {
        Bed bed = bedService.readById(id);
        BedDto bedDto = BeanUtil.toBean(bed, BedDto.class);
        return success(bedDto);
    }

    /**
     * 更新床位
     * @param bedDto
     * @return
     */
    @PutMapping("/update")
    public ResponseResult updateBed(@RequestBody BedDto bedDto){
        Bed bed = BeanUtil.toBean(bedDto, Bed.class);
        bedService.updateBed(bed);
        return success();
    }

    /**
     * 删除床位
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public ResponseResult deleteByIdBed(@PathVariable("id") Long id){
        bedService.deleteByIdBed(id);
        return success();
    }
}
